<script setup lang="ts">
import router from '@/router';
import { ref, onMounted } from 'vue'
import { $DrugList, $DrugType, $Search } from '@/api/drug'


// 分页
const page = ref(0) // 总页数
const changePage = (e) => {
    if (radio.value == 1) {
        $DrugList().then((res) => {
            drugList.value = res.data.slice(0 + 9 * (e - 1), 9 + 9 * (e - 1))
        })
    } else {
        $DrugType({
            classification: radio.value
        }).then((res) => {
            drugList.value = res.data.slice(0 + 9 * (e - 1), 9 + 9 * (e - 1))
        })
    }
}

// 跳转到药品详情
const goToDrugIntroduce = (id) => {
    router.push('/drugIntroduce?id=' + id)
}

//挂载前
const drugList = ref([])
onMounted(() => {
    // 获取药品列表
    $DrugList().then((res) => {
        drugList.value = res.data.slice(0, 9)
        //计算总页数
        page.value = Math.ceil(res.data.length / 9)
    })
})

// 分类选择
const radio = ref(1)
const changeClassification = () => {
    if (radio.value == 1) {
        $DrugList().then((res) => {
            drugList.value = res.data.slice(0, 9)
            //计算总页数
            page.value = Math.ceil(res.data.length / 9)
        })
    } else {
        $DrugType({
            classification: radio.value
        }).then((res) => {
            drugList.value = res.data.slice(0, 9)
            //计算总页数
            page.value = Math.ceil(res.data.length / 9)
        })
    }
}

// 药品搜索
const drugData = ref('')
const searchDrug = () => {
    $Search({
        data: drugData.value
    }).then((res) => {
        drugList.value = res.data.slice(0, 9)
    })
}

</script>

<template>
    <div class="drug-list">
        <div class="drug-title">
            <div class="drug-title-left">药品列表</div>
            <div class="drug-title-right">
                <input class="drug-title-search" type="text" name="" id="" placeholder="搜索药品名称" v-model="drugData">
                <el-icon @click="searchDrug" class="icon">
                    <IEpSearch />
                </el-icon>
            </div>
        </div>
        <div class="drug-list-choice">
            <div class="drug-choice-text">药品分类</div>
            <el-radio-group v-model="radio" @change="changeClassification">
                <el-radio :value="1">全部</el-radio>
                <el-radio :value="2">镇静剂</el-radio>
                <el-radio :value="3">抗生素类</el-radio>
                <el-radio :value="4">抗病毒类</el-radio>
                <el-radio :value="5">抗癌类</el-radio>
            </el-radio-group>
        </div>
        <div class="drug-list-content">
            <div class="content-item" v-for="item in drugList" :key="item.id" @click="goToDrugIntroduce(item.id)">
                <div class="content-item-left">
                    <img class="content-item-img" :src="item.url" alt="">
                </div>
                <div class="content-item-right">
                    <div class="content-item-name">{{ item.name }}</div>
                    <div class="content-item-company">{{ item.company }}</div>
                </div>
            </div>
        </div>
        <el-pagination background layout="prev, pager, next" :page-count="page" @current-change="changePage" />
    </div>
</template>

<style scoped lang="scss">
.drug-list {
    display: flex;
    flex-direction: column;
    width: 80vw;
    margin: 0 auto;
    background-color: #f5f5f5;
    padding: 3vh 2vw;
    box-shadow: 0.2vw 0.2vw 0.2vw #ccc;

    .drug-title {
        display: flex;
        align-items: center;
        justify-content: space-between;

        .drug-title-left {
            display: flex;
            line-height: 3vh;
            font-size: 1.5vw;
            padding-left: 1.5vw;
            border-left: 0.3vw solid #45c998;
        }

        .drug-title-right {
            display: flex;

            .drug-title-search {
                display: flex;
                width: 13vw;
                height: 3.5vh;
                font-size: 0.9vw;
                border-radius: 3.5vh;
                padding-left: 1vw;
                border: 0.1vw solid #ccc;
            }

            .icon {
                top: 0.9vh;
                right: 1.5vw;
                cursor: pointer;
            }
        }
    }

    .drug-list-choice {
        display: flex;
        align-items: center;
        margin: 3vh 0;

        .drug-choice-text {
            font-size: 1.2vw;
            color: #555;
        }

        :deep(.el-radio) {
            height: 100%;
            margin: 0 2vw;


            .el-radio__inner {
                width: 1vw;
                height: 1vw;
                border: none;
            }

            .el-radio__inner::after {
                height: 0.4vw;
                width: 0.4vw;
            }

            .el-radio__label {
                font-size: 1vw;
                padding-left: 1vw;
            }
        }

    }

    .drug-list-content {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;

        .content-item {
            display: flex;
            width: 24vw;
            height: 19vh;
            background-color: #fff;
            color: #333;
            margin: 2vh 0;
            cursor: pointer;
            box-shadow: 0.2vw 0.2vw 0.2vw #ccc;

            .content-item-left {
                display: flex;
                align-items: center;
                padding: 1vw;

                .content-item-img {
                    width: 6vw;
                    height: 6vw;
                }
            }

            .content-item-right {

                .content-item-name {
                    font-size: 1.3vw;
                    margin: 4vh 0 2vh;
                }

                .content-item-company {
                    font-size: 0.9vw;
                }
            }
        }

        .content-item:hover {
            background-color: #45c998;
            color: #fff;
        }
    }

    :deep(.el-pagination) {
        margin-top: 5vh;

        .el-pager li {
            min-width: 2vw;
            height: 2vw;
            font-size: 1vw;
            padding: 0;
            margin: 0 0.5vw;
            border-radius: 0.2vw;
        }

        button {
            min-width: 2vw;
            height: 2vw;
            padding: 0;
            margin: 0 0.5vw;
        }

        .el-icon {
            display: flex;
            font-size: 1vw;

            svg {
                width: 1vw;
            }
        }

    }
}
</style>